package com.wandian.audio.mvvm.audiomusicrankdj;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.bean.MusicBean;
import com.wandian.audio.bean.MusicRankDataBean;
import com.wandian.audio.data.BaseAppApi;
import com.wandian.audio.tools.TokenSignUtils;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description AudioMusicDJRankActivityViewModel
 **/
public class AudioMusicDJRankActivityViewModel extends BaseViewModel {
    public MutableLiveData<List<MusicBean>> listData = new MutableLiveData();
    public AudioMusicDJRankActivityViewModel(@NonNull Application application) {
        super(application);
    }

    //获取音乐排行榜
    public void getMusicRank(String  type){
        if(UIUtils.isEmpty(type)){
            return;
        }
        showDialog();
        //创建Retrofit对象
        Retrofit retrofit = RetrofitUtil.createMusicDJRetrofit();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        Call<Object> call = null;
        switch (type){
            case "人气榜":
                call = request.getMusicRQRank(TokenSignUtils.getMusicRequestMap(new HashMap<>()));
                break;
            case "新歌榜":
                call = request.getMusicNewsRank(TokenSignUtils.getMusicRequestMap(new HashMap<>()));
                break;
            case "收藏榜":
                call = request.getMusicDownloadRank(TokenSignUtils.getMusicRequestMap(new HashMap<>()));
                break;
            case "今日推荐":
                call = request.getMusicTodayRank(TokenSignUtils.getMusicRequestMap(new HashMap<>()));
                break;
        }

        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                dismissDialog();
                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                MusicRankDataBean bean = (MusicRankDataBean) JsonUtil.parseJsonToBean(JsonUtil.toJson(response.body()), MusicRankDataBean.class);
                if(!UIUtils.isEmpty(bean) && !UIUtils.isEmpty(bean.getData())){
                    listData.setValue(bean.getData());
                }else {
                    ToastUtil.show("网络错误");
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                dismissDialog();
                ToastUtil.show("网络错误");
                Log.d("gateMusicRankData", "error="+t.getMessage());
            }
        });

    }
}
